import { create } from 'zustand';
import { persist } from 'zustand/middleware';

// 默认配置
const DEFAULT_CONFIG = {
  autoSave: true,
  fontSize: 14,
  tabSize: 4,
  lineHeight: 1.5,
  fontFamily: 'Monaco,Menlo,"Ubuntu Mono",Consolas,source-code-pro,monospace',
  wordWrap: false,
  showLineNumbers: true,
  showMinimap: false,
  autoComplete: true,
  bracketMatching: true,
};

const useAppStore = create(
  persist(
    (set, get) => ({
      // 主题相关状态
      isDarkTheme: false,
      
      // 全局配置
      config: { ...DEFAULT_CONFIG },
      
      // Actions
      toggleTheme: () => {
        console.log('切换主题');
        set((state) => ({ isDarkTheme: !state.isDarkTheme }));
      },
      
      setTheme: (isDark) => {
        console.log('设置主题:', isDark ? 'dark' : 'light');
        set({ isDarkTheme: isDark });
      },
      
      updateConfig: (newConfig) => {
        console.log('更新配置:', newConfig);
        set((state) => ({
          config: { ...state.config, ...newConfig }
        }));
      },
      
      // 设置字体大小
      setFontSize: (fontSize) => {
        const size = Math.max(8, Math.min(32, fontSize)); // 限制范围 8-32
        set((state) => ({
          config: { ...state.config, fontSize: size }
        }));
      },
      
      // 设置制表符大小
      setTabSize: (tabSize) => {
        const size = Math.max(1, Math.min(8, tabSize)); // 限制范围 1-8
        set((state) => ({
          config: { ...state.config, tabSize: size }
        }));
      },
      
      // 设置行高
      setLineHeight: (lineHeight) => {
        const height = Math.max(1.0, Math.min(3.0, lineHeight)); // 限制范围 1.0-3.0
        set((state) => ({
          config: { ...state.config, lineHeight: height }
        }));
      },
      
      // 切换自动保存
      toggleAutoSave: () => {
        set((state) => ({
          config: { ...state.config, autoSave: !state.config.autoSave }
        }));
      },
      
      // 切换行号显示
      toggleLineNumbers: () => {
        set((state) => ({
          config: { ...state.config, showLineNumbers: !state.config.showLineNumbers }
        }));
      },
      
      // 切换自动换行
      toggleWordWrap: () => {
        set((state) => ({
          config: { ...state.config, wordWrap: !state.config.wordWrap }
        }));
      },
      
      resetConfig: () => {
        console.log('重置配置');
        set({
          config: { ...DEFAULT_CONFIG }
        });
      },
      
      // 获取当前配置的便捷方法
      getConfig: () => get().config,
      getTheme: () => get().isDarkTheme ? 'dark' : 'light',
    }),
    {
      name: 'python-ide-app-store', // localStorage key
      version: 1, // 版本号，用于迁移
      partialize: (state) => ({
        isDarkTheme: state.isDarkTheme,
        config: state.config,
      }),
      // 数据迁移处理
      migrate: (persistedState, version) => {
        if (version < 1) {
          // 如果是旧版本数据，合并默认配置
          return {
            ...persistedState,
            config: { ...DEFAULT_CONFIG, ...persistedState.config }
          };
        }
        return persistedState;
      },
    }
  )
);

export default useAppStore;
